package cn.codinglives.ssoplus.service;

import cn.codinglives.ssoplus.entity.SsoAuth2;
import cn.codinglives.ssoplus.entity.SsoCode;
import cn.codinglives.ssoplus.entity.SsoMain;
import cn.codinglives.ssoplus.entity.SsoSaml;
import cn.codinglives.ssoplus.mapper.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SsoMainService {

    @Resource
    private SsoMainMapper ssoMainMapper;

    @Resource
    private SsoSamlMapper samlMapper;

    @Resource
    private SsoCodeMapper codeMapper;

    @Resource
    private SsoJwtMapper jwtMapper;

    @Resource
    private SsoOauth2Mapper oauth2Mapper;

    /**
     * list sso conf.
     * @return
     */
    public List<SsoMain> list(String protocolType){
        if (StringUtils.isEmpty(protocolType)) {
            return ssoMainMapper.selectList(null);
        }
        QueryWrapper<SsoMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("protocol_type", protocolType);
        return ssoMainMapper.selectList(queryWrapper);
    }


    public SsoSaml getSsoSamlByMainId(Long mainId) {
        QueryWrapper<SsoSaml> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("main_id", mainId);
        SsoSaml saml = samlMapper.selectOne(queryWrapper);
         return saml;
    }

    public SsoAuth2 getSsoAuth2ByMainId(Long mainId) {
        QueryWrapper<SsoAuth2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("main_id", mainId);
        SsoAuth2 auth2 = oauth2Mapper.selectOne(queryWrapper);
        return auth2;
    }

    public SsoCode getSsoCodeByMainId(Long mainId) {
        QueryWrapper<SsoCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("main_id", mainId);
        SsoCode code = codeMapper.selectOne(queryWrapper);
        return code;
    }
}
